package com.cloudlibrary.service.impl;

import com.cloudlibrary.domain.Book;
import com.cloudlibrary.domain.Record;
import com.cloudlibrary.domain.User;
import com.cloudlibrary.entity.PageResult;
import com.cloudlibrary.service.BookService;
import com.cloudlibrary.mapper.BookMapper;
import com.cloudlibrary.service.RecordService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author lenove
* @description 针对表【book】的数据库操作Service实现
* @createDate 2024-12-12 15:23:12
*/
@Service
@Transactional
public class BookServiceImpl implements BookService{
    @Autowired
    private BookMapper bookMapper;

    @Override
    public PageResult selectNewBooks(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page = bookMapper.selectNewBooks();
        return new PageResult(page.getTotal(), page.getResult());
    }
    public Book findById(String id){
        return bookMapper.findById(id);
    }
    @Override
    public Integer borrowBook(Book book){
        Book b = this.findById(book.getId()+"");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        book.setBorrowTime(dateFormat.format(new Date()));
        book.setStatus("1");
        book.setPrice(b.getPrice());
        book.setUploadtime(b.getUploadtime());
        return bookMapper.editBook(book);
    }
    @Override
    public PageResult search(Book book, Integer pageNum, Integer pageSize) {
        // 设置分页查询的参数，开始分页
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page=bookMapper.searchBooks(book);
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public Integer addBook(Book book) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //设置新增图书的上架时间
        book.setUploadtime(dateFormat.format(new Date()));
        return  bookMapper.addBook(book);
    }
    public Integer editBook(Book book) {
        return bookMapper.editBook(book);
    }
    @Override
    public PageResult searchBorrowed(Book book, User user, Integer pageNum, Integer pageSize) {
        // 设置分页查询的参数，开始分页
        PageHelper.startPage(pageNum, pageSize);
        Page<Book> page;
        //将当前登录的用户放入查询条件中
        book.setBorrower(user.getName());
        //如果是管理员，查询自己借阅但未归还的图书和所有待确认归还的图书
        if("ADMIN".equals(user.getRole())){
            page= bookMapper.selectBorrowed(book);
        }else {
            //如果是普通用户，查询自己借阅但未归还的图书
            page= bookMapper.selectMyBorrowed(book);
        }
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Override
    public boolean returnBook(String id,User user) {
        //根据图书id查询出图书的完整信息
        Book book = this.findById(id);
        //再次核验当前登录人员和图书借阅者是不是同一个人
        boolean rb = book.getBorrower().equals(user.getName());
        //如果是同一个人，允许归还
        if (rb) {
            //将图书借阅状态修改为归还中
            book.setStatus("2");
            bookMapper.editBook(book);
        }
        return rb;
    }
//    @Override
//    public Integer returnConfirm(String id) {
//        Book book = this.findById(id);
//        book.setStatus("0");
//        book.setBorrower("");
//        book.setBorrowtime("");
//        book.setReturntime("");
//        return bookMapper.editBook(book);
//    }

    @Autowired
//注入RecordService对象
    private RecordService recordService;
    /**
     * 归还确认
     * @param id 待归还确认的图书id
     */
    @Override
    public Integer returnConfirm(String id) {
        //根据图书id查询图书的完整信息
        Book book = this.findById(id);
        //根据归还确认的图书信息，设置借阅记录
        Record record = this.setRecord(book);
        //将图书的借阅状态修改为可借阅
        book.setStatus("0");
        //清除当前图书的借阅人信息
        book.setBorrower(" ");
        //清除当前图书的借阅时间信息
        book.setBorrowTime(" ");
        //清除当亲图书的预计归还时间信息
        book.setReturntime(" ");
        Integer count= bookMapper.editBook(book);
        //如果归还确认成功，则新增借阅记录
        if(count==1){
            return  recordService.addRecord(record);
        }
        return 0;
    }
    /**
     * 根据图书信息设置借阅记录的信息
     * @param book 借阅的图书信息
     */
    private Record setRecord(Book book){
        Record record=new Record();
        //设置借阅记录的图书名称
        record.setBookname(book.getName());
        //设置借阅记录的图书isbn
        record.setBookisbn(book.getIsbn());
        //设置借阅记录的借阅人
        record.setBorrower(book.getBorrower());
        //设置借阅记录的借阅时间
        record.setBorrowTime(book.getBorrowTime());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //设置图书归还确认的当天为图书归还时间
        record.setRemandTime(dateFormat.format(new Date()));
        return record;
    }
}




